package milkproduction.controller;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import milkproduction.service.ProductionProcessService;
import milkproduction.service.ReleaseService;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class ReleaseController extends MultiActionController{
	private ReleaseService rservice;
	public void setReleaseService(ReleaseService rservice){
		this.rservice = rservice;
	}

	// jsp 페이지를 처음 요청할때 실행되는 controller
	public ModelAndView releasePageloading(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView();
		int totalBox = Integer.parseInt(request.getParameter("totalBox"));
		HashMap map = rservice.checkCurrentState();
		map.put("totalBox", totalBox);
		mv.addAllObjects(map);
		mv.setViewName("releasePage");
		return mv;
	}
	// ajax : 계속해서 데이터를 던져주는 처리를 할 controller
	public ModelAndView continuouslyReferToData(HttpServletRequest request, HttpServletResponse response)throws Exception{
		ModelAndView mv = new ModelAndView();
		// 생산된 박스량
		int todayBox = ProductionProcessService.productMilkBox;
		mv.setViewName("jsonview");
		mv.addObject("todayBox", todayBox);
		return mv;
	}
	
	// ajax : 버튼을 눌렀을 때 처리되는 controller
	public ModelAndView clickReleaseBtn(HttpServletRequest request, HttpServletResponse response)throws Exception{
		ModelAndView mv = new ModelAndView();
		String msg = "";
		
		// orderNo 조회
		int orderNo = Integer.parseInt(request.getParameter("orderNo"));
		
		System.out.println(orderNo);
		// 출고 처리 서비스
		boolean flag = rservice.releaseFinishedProduct(orderNo);
		
		if(flag){	// 출고가 정상적으로 이루어졌을 경우
			msg = "출고가 완료되었습니다.";
		}else{ 		// 출고가 이루어지지 않은 경우
			msg = "출고가 정상적으로 이루어지지 않았습니다.";
		}
		mv.setViewName("jsonview");
		mv.addObject("msg", msg);
		return mv;
	}
}
